package com.zaki.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

public class JWTUtils {
    private static String signKey ="zaki";
    private static Long expireTime =43200000L;

    /**
     * 生成jwt令牌
     * @param map 是jwt第二部分负载payload里面的内容
     * @return
     */
    public static String generateJwt(Map<String,Object> map){
        String jwt = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256,signKey)
                .addClaims(map)
                .setExpiration(new Date(System.currentTimeMillis()+expireTime))
                .compact();
        return jwt;
    }

    /**
     * 解析jwt令牌
     * @param jwt
     * @return
     */
    public static Claims parseJwt(String jwt){
        Claims claims =Jwts.parser()
                            .setSigningKey(signKey)
                            .parseClaimsJws(jwt)
                            .getBody();
        return claims;
    }
}
